<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 退款单操作类 billRefund
 * @package  htdocs
 * @subpackage  module/base
 * @author   allen.qiang
 * @version 1.0
 */

class billRefund {

    private $dbca;

    public function __construct( &$dbca ) {
        $this->dbca = $dbca;
    }


    /**
     * 查询退款单信息
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *      'sortname' => string 排序字段
     *      'sortorder' => string 排序方式
     *      'page' => int 分页页码
     *      'rp' => int 分页页数
     * )
     * @return null|array
     */
    public function searchBillRefund (&$arg_array){
        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //订单号
        if (isset($arg_array['bill_no']) && is_numeric($arg_array['bill_no'])) {
            $sql .= ' AND bill_no = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['bill_no']);
        }

        //退款单号
        if (isset($arg_array['refund_no']) && !empty($arg_array['refund_no'])) {
            $sql .= ' AND refund_no = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['refund_no'];
        }

        //订单编号
        if (isset($arg_array['bill_code']) && !empty($arg_array['bill_code'])) {
            $sql .= ' AND bill_code LIKE ?';
            $params_type .= 's';
            $params_array[] = '%'.trim($arg_array['bill_code']);
        }

        //会员名
        if (isset($arg_array['custID']) && !empty($arg_array['custID']) ) {
            $sql .= ' AND custID LIKE ?';
            $params_type .= 's';
            $params_array[] = "%".trim($arg_array['custID'])."%";
        }

        //退货单号
        if (isset($arg_array['return_no']) && !empty($arg_array['return_no'])) {
            $sql .= ' AND return_no = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['return_no']);
        }

        //退款来源
        if (isset($arg_array['refund_source']) && is_numeric($arg_array['refund_source']) && $arg_array['refund_source'] > 0 ) {
            $sql .= ' AND refund_source = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['refund_source'];
        }


        //创建开始时间
        if (isset($arg_array['startDate']) && !empty($arg_array['startDate']) ){
            $sql .= ' AND createTime > ?';
            $params_type .= 's';
            $params_array[] = $arg_array['startDate'] . ' 00:00:00';
        }

        //创建结束时间
        if (isset($arg_array['endDate']) && !empty($arg_array['endDate']) ){
            $sql .= ' AND createTime < ?';
            $params_type .= 's';
            $params_array[] = $arg_array['endDate'] . ' 23:59:59';
        }
        //退款状态
        if (isset($arg_array['refund_status']) && !empty($arg_array['refund_status']) ){
            $sql .= ' AND refund_status = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['refund_status'] ;
        }


        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4);
        }


        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 0) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM bill_refund ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM bill_refund ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);

            if ($temp_item_array) {

                foreach ($temp_item_array as &$item) {
                    $item_array[] = array(
                        'createTime' => $item->createTime,
                        'refund_no' => $item->refund_no,
                        'bill_no' => $item->bill_no,
                        'return_no' => $item->return_no,
                        'bill_code' => $item->bill_code,
                        'custID' => $item->custID,
                        'payTypeID' => $item->payTypeID,
                        'payTypeName' => $item->payTypeName,
                        'refund_type_name' => $item->refund_type_name,
                        'refund_account' => $item->refund_account,
                        'payee' => $item->payee,
                        'refund_amount' => $item->refund_amount,
                        'refund_source' => $item->refund_source,
                        'refund_status' => $item->refund_status,
                        'remark' => $item->remark,
                        'reject_cause' => $item->reject_cause,//驳回原因
                        'reason_refund' => $item->reason_refund,//退款原因
                        'to_city' => $item->to_city,//银行卡归属城市
                        'bank_deposit' => $item->bank_deposit, //开户行
                        'print_num'=>$item->print_num//打印次数
                    );
                }
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;
        }
    }

    /**
     * 查询退货商品信息
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
    'return_no' => int 退货单号
    'sortname' => string 排序字段
    'sortorder' => string 排序方式
    'page' => int 分页页码
    'rp' => int 分页页数
     * )
     * @return null|array
     */
    public function searchBillReturnGoods( &$arg_array ) {
    global $G_SHOP;
        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //订单号
        if (isset($arg_array['return_no']) && !empty($arg_array['return_no'])) {
            $sql .= ' AND return_no = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['return_no'];
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4);
        }


        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 0) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM bill_return_goods ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM bill_return_goods ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);

            if (!empty($temp_item_array)) {
                foreach ($temp_item_array as &$item) {
                    $sql_fittings='select * from bill_goods_fittings where bill_goods_no =?';
                    $bill_goods_no=$G_SHOP->DBCA->stmt_query($sql_fittings,'i',$item->bill_goods_no);
                    $buy_momney=0;
                    foreach($bill_goods_no as $bill_buy){
                        $buy_momney+=$bill_buy->buyMoney*$bill_buy->buyNum;
                    }
                    $item_array[] = array(
                        'return_no' => $item->return_no,
                        'bill_goods_no' => $item->bill_goods_no,
                        'goods_goodsID' => $item->goods_goodsID,
                        'goods_postID' => $item->goods_postID,
                        'goods_name' => $item->goods_name,
                        'goods_spec' => $item->goods_spec,
                        'goods_amount' => $item->goods_amount,
                        'goods_storePrice' => $item->goods_storePrice,
                        'goods_purchasePrice' => $item->goods_purchasePrice,
                        'goods_pointsPrice' => $item->goods_pointsPrice,
                        'goods_points' => $item->goods_points,
                        'buyMoney'=>$buy_momney
                    );
                }
            }
            if (!empty($item_array)) {
                return $item_array;
            }


        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;
        }

    }


    /**
     * 保存退款单
     * @param array $arg_array
     * array(           保存参数
     *  'operator' => string  操作人  [必须]
     *  'refund_source' => int  退款来源  [必须]
     *  'refund_amount' => decimal  退款金额  [必须]
     *  'refund_no' => int  退款单号
     *  'bill_no' => int  订单号
     *  'return_no' => int  退货单号
     *  'custID' => string  会员名
     *  'pcustID' => int  客户号
     *  'payTypeID' => string  支付方式ID
     *  'payTypeName' => string  支付方式名称
     *  'refund_type_name' => string  退款方式
     *  'refund_account' => string  退款账户
     *  'payee' => string  收款人
     *  'remark' => string  备注
     * )
     * @param bool $transaction
     * @return array
     */
    public function saveBillRefund($arg_array , $transaction = true){
         global $G_PGLOB;
        quanli_require('module/base/common/quanliCommon.class.php');

        //全站公共类
        $commHandler = new commHandler($this->dbca);

        if(!isset($arg_array['refund_source'] , $arg_array['refund_amount']) || !is_numeric($arg_array['refund_source']) || !is_money($arg_array['refund_amount'])  ){
            return array('code' => 400 , 'message' => '参数错误');
        }


        try{

            quanli_require('module/base/customer/logHandler.class.php');

            //日志类
            $logHandler = new logHandler( $this->dbca );


            $bill_refund = new entity('bill_refund');
            if(!isset($arg_array['refund_no']) || !is_numeric($arg_array['refund_no']) ){       //走新建
                //生码
                //mod by allen.qinag 20150420 取seqno优化 start
                //$arg_array['refund_no'] = $commHandler->generate_seqno("seqno_refund");
                $arg_array['refund_no'] = generate_seqno("seqno_refund");
                //mod by allen.qinag 20150420 取seqno优化 end
                $bill_refund->refund_no = $arg_array['refund_no'];
                $extend_1 = "退款单建立";        //日志记录内容
            }else{              //走审核
                $bill_refund->refund_no = $arg_array['refund_no'];
                if(!$this->dbca->fetch($bill_refund)){
                    //throw new Exception("未找到退款单信息" , 404);
                    return array( 'code' => 404 , 'message' => '未找到退款单信息' );
                }

                if($bill_refund->refund_status == 2){
                    //throw new Exception("该退款单已审核，无法修改" , 406);
                    return array( 'code' => 406 , 'message' => '该退款单已审核，无法修改' );
                }
                if($bill_refund->refund_status == 3){
                    //throw new Exception("该退款单已取消，无法修改" , 406);
                    return array( 'code' => 406 , 'message' => '该退款单已取消，无法修改' );
                }
                if($bill_refund->refund_status == 4){
                    return array( 'code' => 406 , 'message' => '该退款单已过账，无法修改' );
                }
                $extend_1 = "退款单修改";
            }

            $bill_refund->creator = $arg_array['operator'];
            $bill_refund->bill_no = isset($arg_array['bill_no']) && is_numeric($arg_array['bill_no']) ? $arg_array['bill_no'] : null;
            $bill_refund->bill_code = isset($arg_array['return_bill_code']) ? $arg_array['return_bill_code'] : null;
            $bill_refund->return_no = isset($arg_array['return_no']) && !empty($arg_array['return_no']) ? $arg_array['return_no'] : null;
            $bill_refund->custID = isset($arg_array['custID']) ? $arg_array['custID'] : null;
            $bill_refund->pcustID = isset($arg_array['pcustID']) ? $arg_array['pcustID'] : null;
            $bill_refund->payTypeID = isset($arg_array['payTypeID']) ? $arg_array['payTypeID'] : null;
            $bill_refund->payTypeName = isset($arg_array['payTypeName']) ? $arg_array['payTypeName'] : null;
            $bill_refund->refund_type_name = isset($arg_array['refund_type_name']) ? $arg_array['refund_type_name'] : null;
            $bill_refund->refund_account = isset($arg_array['refund_account']) ? $arg_array['refund_account'] : null;
            $bill_refund->payee = isset($arg_array['payee']) ? $arg_array['payee'] : null;
            $bill_refund->refund_amount = $arg_array['refund_amount'];
            $bill_refund->return_time = null;
            $bill_refund->remark = isset($arg_array['remark']) ? $arg_array['remark'] : null;
            $bill_refund->refund_source = $arg_array['refund_source'];
            $bill_refund->refund_status = 1;
            $bill_refund->reason_refund = $arg_array['reason_refund'];//退款原因
            $bill_refund->to_city = $arg_array['to_city'];//银行卡归属城市
            $bill_refund->bank_deposit = $arg_array['bank_deposit'];//开户行

            //开启事务
            if($transaction){
                $this->dbca->beginTransaction();
            }
             //修改退款单
            $refund_log="";
            if($bill_refund->uuid){
               $sel_bill_refund='select * from bill_refund where refund_no=? ';
               $refund_info=$this->dbca->stmt_query($sel_bill_refund,'s',  $arg_array['refund_no']);
//               error_log(print_r($refundInfo[0]->uuid,1));
//                error_log('XXXXXXXXXXXXXXXXXXXXXX');
//                error_log(print_r($bill_refund,1));
                if($refund_info[0]->bill_code!=$bill_refund->bill_code){
                    $refund_log.="<p>订单编号修改：". $refund_info[0]->bill_code."=>".$bill_refund->bill_code."</p>";
                }
                if($refund_info[0]->refund_source!=$bill_refund->refund_source){
                    $refund_log.="<p>退单来源修改：".$refund_info[0]->refund_source==1 ? "订单退货" : ($refund_info[0]->refund_source==2 ? "订单取消" : "其它")."=>". $bill_refund->refund_source==1 ? "订单退货" : ($bill_refund->refund_source==2 ? "订单取消" : "其它")."</p>";
                }
                if($refund_info[0]->reason_refund!=$bill_refund->reason_refund){
                    $refund_log.="<p>退款原因修改：".$refund_info[0]->reason_refund."=>".$bill_refund->reason_refund."</p>";
                }
                if($refund_info[0]->return_no!=$bill_refund->return_no){
                    $refund_log.="<p>退货单号修改：".$refund_info[0]->return_no."=>".$bill_refund->return_no."</p>";
                }
                if($refund_info[0]->payee!=$bill_refund->payee){
                    $refund_log.="<p>收款人修改：".$refund_info[0]->payee."=>".$bill_refund->payee."</p>";
                }
                if($refund_info[0]->refund_account!=$bill_refund->refund_account){
                    $refund_log.="<p>退款账号修改：".$refund_info[0]->refund_account."=>".$bill_refund->refund_account."</p>";
                }
                if($refund_info[0]->to_city!=$bill_refund->to_city){
                    $refund_log.="<p>银行卡归属城市修改：".$refund_info[0]->to_city."=>".$bill_refund->to_city."</p>";
                }
                if($refund_info[0]->bank_deposit!=$bill_refund->bank_deposit){
                    $refund_log.="<p>开户行修改：".$refund_info[0]->bank_deposit."=>".$bill_refund->bank_deposit."</p>";
                }
                if($refund_info[0]->refund_amount!=$bill_refund->refund_amount){
                    $refund_log.="<p>退款金额修改：".$refund_info[0]->refund_amount."=>".$bill_refund->refund_amount."</p>";
                }
                if($refund_info[0]->remark!=$bill_refund->remark){
                    $refund_log.="<p>备注修改：".$refund_info[0]->remark."=>".$bill_refund->remark."</p>";
                }
            }
            //保存退款主表信息
            $this->dbca->save($bill_refund);

           $sql_cust='select * from user_master where userID=?';
           $cust=$G_PGLOB->DBCA->stmt_query($sql_cust,'s',$arg_array['operator']);

            //退款单日志参数
            $log_array = array(
                'logCategoryID' => 11,//回款日志编号
                'logItem' => $bill_refund->refund_no,//退款单号
                'extend_1' => $extend_1,//操作名称（退款单建立OR修改）
                'extend_5' => $bill_refund->refund_status,//退款单状态
                'operator' =>$cust[0]->trueName,//创建人
                'extend_2'=>$refund_log, //具体操作内容
            );

            $result_log = $logHandler->saveLogInfo($log_array);

            if($result_log['code'] != 200 ){
                throw new Exception("链接超时，请重试" , 406);
            }

            if($transaction){
                $this->dbca->commit();
            }

            return array('code' => 200 , 'message' => 'ok');

        }catch (Exception $e){

            if($transaction){
                $this->dbca->rollBack();
            }

            if( in_array($e->getCode(),array(404,406)) ){
                return array('code' => $e->getCode() , 'message' => $e->getMessage());
            }else{
                error_log($e->getMessage());
                return array('code' => 500 , 'message' => '系统错误，保存失败');
            }
        }

    }

    /**
     * 审核退款单
     */
    public function auditBillRefund($arg_array){
            global $G_PGLOB;
        if(!isset($arg_array['refund_no']) || !is_numeric($arg_array['refund_no']) ){
            return array( 'code' => 400 , 'message' => '参数错误' );
        }
        try{

            //日志类
            $logHandler = new logHandler($this->dbca);

            $bill_refund = new entity('bill_refund');
            $bill_refund->refund_no = $arg_array['refund_no'];
            if(!$this->dbca->fetch($bill_refund)){
               // throw new Exception("未找到退款单信息" , 404);
                return array( 'code' => 404 , 'message' => '未找到退款单信息' );
            }

            if($bill_refund->refund_status == 2){
                return array( 'code' => 406 , 'message' => '该退款单已审核，不能重复审核' );
               // throw new Exception("该退款单已审核，不能重复审核" , 406);
            }
            if($bill_refund->refund_status == 3){
                //throw new Exception("该退款单已取消，无法审核" , 406);
                return array( 'code' => 406 , 'message' => '该退款单已取消，无法审核' );
            }
            if($bill_refund->refund_status == 4){
                return array( 'code' => 406 , 'message' => '该退款单已过账，无法审核' );
            }

            $bill_refund->refund_status = 2;
            $bill_refund->return_time = date('Y-m-d H:i:s');
            //开启事务
            $this->dbca->beginTransaction();

            //更新退款单主表
            $this->dbca->save($bill_refund);

            $sql_cust='select * from user_master where userID=?';
            $cust=$G_PGLOB->DBCA->stmt_query($sql_cust,'s',$arg_array['operator']);
            //退款单日志参数
            $log_array = array(
                'logCategoryID' => 1,
                'logItem' => $bill_refund->refund_no,
                'extend_1' => "退款单审核",
                'extend_5' => $bill_refund->refund_status,
                'operator' => $cust[0]->trueName
            );
            $result_log = $logHandler->saveLogInfo($log_array);
            if($result_log['code'] != 200 ){
                throw new Exception("链接超时，请重试" , 406);
            }

            $this->dbca->commit();

            return array( 'code' => 200 , 'message' => '审核成功');

        }catch (Exception $e){

            $this->dbca->rollBack();

            if( in_array($e->getCode(),array(404,406))){
                return array('code' => $e->getCode() , 'message' => $e->getMessage());
            }else{
                error_log($e->getMessage());
                return array('code' => 500 , 'message' => '系统错误，审核失败');
            }
        }

    }

    /**
     * 退款单过账
     */
    public function postingBillRefund($arg_array){
            global  $G_PGLOB;
        if(!isset($arg_array['refund_no']) || !is_numeric($arg_array['refund_no']) ){
            return array( 'code' => 400 , 'message' => '参数错误' );
        }

        try{

            //日志类
            $logHandler = new logHandler($this->dbca);

            $bill_refund = new entity('bill_refund');
            $bill_refund->refund_no = $arg_array['refund_no'];
            if(!$this->dbca->fetch($bill_refund)){
                // throw new Exception("未找到退款单信息" , 404);
                return array( 'code' => 404 , 'message' => '未找到退款单信息' );
            }

            $refunded_amount = $bill_refund->refund_amount;

            if($bill_refund->refund_status == 1){
                //throw new Exception("该退款单未审核，不能过账" , 406);
                return array( 'code' => 406 , 'message' => '该退款单未审核，不能过账' );
            }
            if($bill_refund->refund_status == 3){
                //throw new Exception("该退款单已取消，不能过账" , 406);
                return array( 'code' => 406 , 'message' => '该退款单已取消，不能过账' );
            }
            if($bill_refund->refund_status == 4){
                //throw new Exception("该退款单已过账，不能重复过账" , 406);
                return array( 'code' => 406 , 'message' => '该退款单已过账，不能重复过账' );
            }

            $bill_refund->refund_status = 4;
            $bill_refund->posting_person = $arg_array['operator'];
            $bill_refund->posting_time = date('Y-m-d H:i:s');
            //开启事务
            $this->dbca->beginTransaction();


            //查询订单信息
            $sql = "SELECT bill_online_money,bill_received_money FROM bill_master WHERE bill_code = ?";
            $bill_array = $G_PGLOB->DBCA->stmt_query($sql , 's' , $bill_refund->bill_code);

            $amount_received = $bill_array[0]->bill_received_money;
            if($_REQUEST['refund_no']){
                //查询次订单是否被申请退款过
                $refund_sql="select * from  bill_refund where bill_code=? and refund_status =4 and refund_no!=?";
                $refund_query = $G_PGLOB->DBCA->stmt_query($refund_sql , 'si' , $bill_refund->bill_code, $bill_refund->refund_no);
            }else{
                //查询次订单是否被申请退款过
                $refund_sql="select * from  bill_refund where bill_code=? and refund_status =4";
                $refund_query = $G_PGLOB->DBCA->stmt_query($refund_sql , 's' , $bill_refund->bill_code);
            }

            $refund_money=0;//初始化已申请的退款金额
            foreach($refund_query as  $refund_array){
                $refund_money+=$refund_array->refund_amount;
            }
            //实际可退金额
            $refund_money_1=$amount_received-$refund_money;
            if($refund_money_1 < $refunded_amount){
                throw new Exception("退款金额不能大于已付款金额" , 406);
            }

            //更新退款单主表
            $this->dbca->save($bill_refund);

            $sql_cust='select * from user_master where userID=?';
            $cust=$G_PGLOB->DBCA->stmt_query($sql_cust,'s',$arg_array['operator']);

            //退款单日志参数
            $log_array = array(
                'logCategoryID' => 1,
                'logItem' => $bill_refund->refund_no,
                'extend_1' => "退款单过账",
                'extend_5' => $bill_refund->refund_status,
                'operator' => $cust[0]->trueName
            );
            $result_log = $logHandler->saveLogInfo($log_array);
            if($result_log['code'] != 200 ){
                throw new Exception("链接超时，请重试" , 406);
            }

            $this->dbca->commit();

            return array( 'code' => 200 , 'message' => '过账成功');

        }catch (Exception $e){

            $this->dbca->rollBack();

            if( in_array($e->getCode(),array(404,406))){
                return array('code' => $e->getCode() , 'message' => $e->getMessage());
            }else{
                error_log($e->getMessage());
                return array('code' => 500 , 'message' => '系统错误，过账失败');
            }
        }

    }

    /**
     * 驳回操作
     */
    public function rejectBillRefund( $arg_array ){
        global $G_PGLOB;

        if(!isset($arg_array['refund_no']) || !is_numeric($arg_array['refund_no']) ){
            return array( 'code' => 400 , 'message' => '参数错误' );
        }

        try{

            //日志类
            $logHandler = new logHandler($this->dbca);

            $bill_refund = new entity('bill_refund');
            $bill_refund->refund_no = $arg_array['refund_no'];
            if(!$this->dbca->fetch($bill_refund)){
                //throw new Exception("未找到退款单信息" , 404);
                return array( 'code' => 404 , 'message' => '未找到退款单信息' );
            }

            if(in_array($bill_refund->refund_status,array(1,2,3))){
                //throw new Exception("该退款单未过账，不能驳回" , 406);
                return array( 'code' => 406 , 'message' => '该退款单未过账，不能驳回' );
            }

            $bill_refund->refund_status = 1;
            $bill_refund->reject_cause = $arg_array['reject_cause'];

            //开启事务
            $this->dbca->beginTransaction();

            //更新退款单主表
            $this->dbca->save($bill_refund);
            $sql_cust='select * from user_master where userID=?';
            $cust=$G_PGLOB->DBCA->stmt_query($sql_cust,'s',$arg_array['operator']);

            //退款单日志参数
            $log_array = array(
                'logCategoryID' => 1,
                'logItem' => $bill_refund->refund_no,
                'extend_1' => "退款单驳回",
                'extend_5' => $bill_refund->refund_status,
                'operator' =>$cust[0]->trueName ,
                'extend_2'=> "驳回原因：".$arg_array['reject_cause']
            );
            $result_log = $logHandler->saveLogInfo($log_array);
            if($result_log['code'] != 200 ){
                throw new Exception("链接超时，请重试" , 406);
            }

            $this->dbca->commit();

            return array( 'code' => 200 , 'message' => '驳回成功');

        }catch (Exception $e){

            $this->dbca->rollBack();

            if( in_array($e->getCode(),array(404,406))){
                return array('code' => $e->getCode() , 'message' => $e->getMessage());
            }else{
                error_log($e->getMessage());
                return array('code' => 500 , 'message' => '系统错误，驳回失败');
            }
        }

    }

    /**
     * 取消退款单
     */
    public function cancleBillRefund ($arg_array){
        global $G_PGLOB;

        if(!isset($arg_array['refund_no']) || !is_numeric($arg_array['refund_no']) ){
            return array( 'code' => 400 , 'message' => '参数错误' );
        }

        try{

            //日志类
            $logHandler = new logHandler($this->dbca);

            $bill_refund = new entity('bill_refund');
            $bill_refund->refund_no = $arg_array['refund_no'];
            if(!$this->dbca->fetch($bill_refund)){
                //throw new Exception("未找到退款单信息" , 404);
                return array( 'code' => 404 , 'message' => '未找到退款单信息' );
            }

            if($bill_refund->refund_status == 2){
                //throw new Exception("该退款单已审核，取消失败" , 406);
                return array( 'code' => 406 , 'message' => '该退款单已审核，无法取消' );
            }
            if($bill_refund->refund_status == 3){
                //throw new Exception("该退款单已取消，不能重复取消" , 406);
                return array( 'code' => 406 , 'message' => '该退款单已取消，不能重复取消' );
            }
            if($bill_refund->refund_status == 4){
                return array( 'code' => 406 , 'message' => '该退款单已过账，无法取消' );
            }

            $bill_refund->refund_status = 3;

            //开启事务
            $this->dbca->beginTransaction();

            //更新退款单主表
            $this->dbca->save($bill_refund);

            $sql_cust='select * from user_master where userID=?';
            $cust=$G_PGLOB->DBCA->stmt_query($sql_cust,'s',$arg_array['operator']);

            //退款单日志参数
            $log_array = array(
                'logCategoryID' => 1,
                'logItem' => $bill_refund->refund_no,
                'extend_1' => "退款单取消",
                'extend_5' => $bill_refund->refund_status,
                'operator' => $cust[0]->trueName
            );
            $result_log = $logHandler->saveLogInfo($log_array);
            if($result_log['code'] != 200 ){
                throw new Exception("链接超时，请重试" , 406);
            }

            $this->dbca->commit();

            return array( 'code' => 200 , 'message' => '取消成功');

        }catch (Exception $e){

            $this->dbca->rollBack();

            if( in_array($e->getCode(),array(404,406))){
                return array('code' => $e->getCode() , 'message' => $e->getMessage());
            }else{
                error_log($e->getMessage());
                return array('code' => 500 , 'message' => '系统错误，取消失败');
            }
        }

    }

}